package com.costeaalex.blueserver.setup;

import javax.comm.CommPort;

import com.costeaalex.blueserver.bluecove.BlueCoveServer;
import com.costeaalex.blueserver.comm.COMPortClientFinder;
import com.costeaalex.blueserver.comm.COMServer;
import com.costeaalex.blueserver.interfaces.MainFrame;
import com.costeaalex.blueserver.interfaces.manager.ScreenManager;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupBlueCove;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupBluetoothOn;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupFinished;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupOpeningPorts;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupPortsOpened;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupSelectConnection;
import com.costeaalex.blueserver.interfaces.setup.ScreenSetupStartupRegister;
import com.costeaalex.blueserver.settings.AppSettingsSet;
import com.costeaalex.blueserver.settings.Settings;
import com.costeaalex.blueserver.systemtray.SystemTrayManager;
import com.costeaalex.blueserver.threads.MainThread;
import com.costeaalex.blueserver.timeout.HandleTimeoutProcess;

public class SetupServerApp extends Thread
	{
	
	private MainThread mTL;
	private HandleTimeoutProcess oTFCP;
	private COMPortClientFinder cPF;
	private BlueCoveServer bCS;
	private COMServer cS;
	private MainFrame mFL;
	private int setupCode=0;
	//private boolean serverOK=false;
	private boolean portsAreOpened=false;
	private Settings set;
	private AppSettingsSet aSS;
	private SystemTrayManager sTM;
	
	public int tryServer=0;
	public int registerStartup=0;
	
	private ScreenManager sM;
	private ScreenSetupSelectConnection connectionChoose;
	private ScreenSetupOpeningPorts	openingPorts;
	private ScreenSetupPortsOpened portsOpened;
	private ScreenSetupFinished setupFinished;
	private ScreenSetupBluetoothOn sBO;
	private ScreenSetupStartupRegister sSSR;
	private ScreenSetupBlueCove sSBC;
		
	public SetupServerApp(MainThread mT, MainFrame mF, int sC)
		{
		mTL=mT;
		mFL=mF;
		setupCode=sC;
		set=new Settings();
		connectionChoose=new ScreenSetupSelectConnection(this);
		sBO=new ScreenSetupBluetoothOn(this);
		sM=new ScreenManager(mFL);
		sTM=new SystemTrayManager();
		aSS=new AppSettingsSet();
		setupFinished=new ScreenSetupFinished();
		}
	
	@Override
	public void run()
		{
		sM.start();
		if(setupCode==0)
			{
			sM.setNextScreen(sBO,1000);
			sM.toNextScreen(true);
			}
		while(tryServer==0)
			{
			try
				{
				sleep(1000);
				} 
			catch (InterruptedException e)
				{
				e.printStackTrace();
				}
			}
		
		if(tryServer==1)
			FindCOMPort();
		if(tryServer==2)
			BlueCoveServer();
		}
	
	public void startSetup()
		{
		sM.setNextScreen(connectionChoose,10);
		sM.toNextScreen(true);
		}
	
	public void FindCOMPort()
		{
		if(!portsAreOpened)
			{
			openingPorts=new ScreenSetupOpeningPorts();
			sM.setNextScreen(openingPorts,1000);
			sM.toNextScreen(true);
			}
		oTFCP=new HandleTimeoutProcess(180000, cPF);
		oTFCP.start();	
		cPF=new COMPortClientFinder(this);
		cPF.start();
		}
	
	public void COMPortFound(CommPort cP)
		{
		cS=new COMServer(cP);
		cS.start();
		cS.stop();
		set.connType=1;
		set.port=cP.getName();
		if(sTM.trayAvailable())
			set.systemTrayEnabled=true;
		//serverOK=true;
		
		sSSR=new ScreenSetupStartupRegister(this);
		sM.setNextScreen(sSSR,500);
		sM.toNextScreen(true);
		registerStartup();
		}
	
	public void portsOpened()
		{
		portsAreOpened=true;
		portsOpened=new ScreenSetupPortsOpened();
		sM.setNextScreen(portsOpened,500);
		sM.toNextScreen(true);
		}
	
	public void BlueCoveServer()
		{
		sSBC=new ScreenSetupBlueCove();
		sM.setNextScreen(sSBC,500);
		sM.toNextScreen(true);
		
		bCS=new BlueCoveServer();
		bCS.start();
		bCS.stop();
		set.connType=2;
		set.port="";
		if(sTM.trayAvailable())
			set.systemTrayEnabled=true;
		//serverOK=true;
		
		sSSR=new ScreenSetupStartupRegister(this);
		sM.setNextScreen(sSSR,500);
		sM.toNextScreen(true);
		registerStartup();
		}
	
	public void registerStartup()
		{
		while(registerStartup==0)
			{
			try
				{
				sleep(500);
				} 
			catch (InterruptedException e)
				{
				e.printStackTrace();
				}
			}
		if(registerStartup==1)
			{
			//TODO register class
			set.startupRegistered=true;
			}
		
		if(registerStartup==2)
			{
			set.startupRegistered=false;
			}
		
		sM.setNextScreen(setupFinished, 500);
		sM.toNextScreen(true);
		setupFinish();
		}
	
	private void setupFinish()
		{
		aSS.setSettings(set);
		mTL.externalStartServer();
		}

	}
